<template>
  <!-- 搜索区域 -->
  <ContentWrap>
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="100px"
    >
      <el-form-item label="模板编码" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入模板编码"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="短信类型" prop="type">
        <el-select
          v-model="queryParams.type"
          placeholder="请选择短信类型"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in system_sms_business_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!--      <el-form-item label="模板名称" prop="name">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.name"-->
      <!--          placeholder="请输入模板名称"-->
      <!--          clearable-->
      <!--          @keyup.enter="handleQuery"-->
      <!--          class="!w-240px"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label="短信渠道" prop="channelId">
        <el-select
          v-model="queryParams.channelId"
          placeholder="请选择短信渠道编号"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="channel in channelList"
            :key="channel.id"
            :value="channel.id"
            :label="
              channel.signature +
              `【${getDictLabel(DICT_TYPE.system_sms_channel_type, channel.code)}】`
            "
          />
        </el-select>
      </el-form-item>
      <el-form-item label="渠道模版编号" prop="apiTemplateId">
        <el-input
          v-model="queryParams.apiTemplateId"
          placeholder="请输入渠道模版编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="开启状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择开启状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in common_enable_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['system:sms-template:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['system:sms-template:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表区域 -->
  <ContentWrap>
    <el-table v-loading="listLoading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="编号" prop="id" width="100" />
      <el-table-column label="模板编码" prop="code" />
      <el-table-column label="模板名称" prop="name" width="200" />
      <el-table-column label="模板内容" prop="content" />
      <el-table-column label="短信类型" prop="type" width="100">
        <template #default="scope">
          <dict-tag :options="system_sms_business_type" :value="scope.row.type" />
        </template>
      </el-table-column>
      <el-table-column label="开启状态" prop="status" width="100">
        <template #default="scope">
          <dict-tag :options="common_enable_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="渠道模版编号" prop="apiTemplateId" width="200" />
      <el-table-column label="短信渠道" prop="channelId" width="150">
        <template #default="scope">
          <el-tooltip :content="'签名：' + scope.row.channelSignature" placement="top-start">
            <dict-tag :options="system_sms_channel_type" :value="scope.row.channelCode" />
          </el-tooltip>
        </template>
      </el-table-column>
      <!--      <el-table-column label="短信渠道编码" prop="channelCode" />-->
      <el-table-column label="创建时间" prop="createTime" width="180px" />
      <el-table-column label="操作" width="180" fixed="right">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['system:sms-template:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="primary"
            @click="openSendForm(scope.row.id)"
            v-hasPermi="['system:sms-template:send-sms']"
          >
            测试
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['system:sms-template:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页区域 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <SmsTemplateForm ref="editFormRef" @success="getList" />
  <!-- 表单弹窗：测试发送 -->
  <SmsTemplateSendForm ref="sendFormRef" />
</template>

<script setup lang="ts">
import { SmsTemplateApi, SmsTemplateVO } from '@/api/system/sms/sms_template'
import { SmsChannelApi, SmsChannelVO } from '@/api/system/sms/sms_channel'
import SmsTemplateForm from './SmsTemplateForm.vue'
import SmsTemplateSendForm from './SmsTemplateSendForm.vue'
import { DICT_TYPE, getDictLabel } from '@/utils/dict'
import download from '@/utils/download'

/** 短信模板 列表 */
defineOptions({ name: 'SystemSmsTemplate' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const { system_sms_business_type, system_sms_channel_type } = useDict(
  DICT_TYPE.system_sms_business_type,
  DICT_TYPE.system_sms_channel_type
)
const { common_enable_status } = useIntDict(DICT_TYPE.common_enable_status)

const listLoading = ref(true) // 列表的加载中状态
const list = ref<SmsTemplateVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  type: undefined,
  code: undefined,
  name: undefined,
  content: undefined,
  apiTemplateId: undefined,
  channelId: undefined,
  status: undefined,
  createTime: [],
  pageNo: 1,
  pageSize: 10
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中
const channelList = ref<SmsChannelVO[]>([]) // 短信渠道列表

/** 查询列表 */
const getList = async () => {
  listLoading.value = true
  try {
    const data = await SmsTemplateApi.getSmsTemplatePage(queryParams)
    list.value = data.content
    total.value = data.totalElement
  } finally {
    listLoading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const editFormRef = ref<InstanceType<typeof SmsTemplateForm>>()
const openForm = (type: string, id?: number) => {
  editFormRef.value?.open(type, id)
}

/** 测试发送操作 */
const sendFormRef = ref<InstanceType<typeof SmsTemplateSendForm>>()
const openSendForm = (id: number) => {
  sendFormRef.value?.open(id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 弹出确认框
    await message.delConfirm()
    // 发起删除请求
    await SmsTemplateApi.deleteSmsTemplate(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await SmsTemplateApi.exportSmsTemplate(queryParams)
    download.excel(data, '短信模板数据.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(async () => {
  await getList()
  channelList.value = await SmsChannelApi.getSmsChannelSimpleList()
})
</script>

<style scoped lang="scss"></style>
